package com.whfc.openapi.api.fse.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.whfc.common.constant.QueueConst;
import com.whfc.common.enums.DevicePlatform;
import com.whfc.common.result.ListData;
import com.whfc.common.util.MathUtil;
import com.whfc.fse.dto.*;
import com.whfc.fse.enums.FseWarnRuleType;
import com.whfc.fse.param.FseLiftDataAddParam;
import com.whfc.fse.service.FseLiftDataService;
import com.whfc.fse.service.FseLiftService;
import com.whfc.fse.service.FseRecordService;
import com.whfc.fvs.dto.FvsStreamDTO;
import com.whfc.fvs.service.FvsDeviceService;
import com.whfc.openapi.api.fse.service.OpenApiFseLiftService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClasssName OpenApiFseLiftServiceImpl
 * @Description 塔机服务类
 * @Author hw
 * @Date 2021/2/4 16:31
 * @Version 1.0
 */
@Service
public class OpenApiFseLiftServiceImpl implements OpenApiFseLiftService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @DubboReference(interfaceClass = FseLiftService.class, version = "1.0.0")
    private FseLiftService fseLiftService;

    @DubboReference(interfaceClass = FseLiftDataService.class, version = "1.0.0")
    private FseLiftDataService fseLiftDataService;

    @DubboReference(interfaceClass = FseRecordService.class, version = "1.0.0")
    private FseRecordService fseRecordService;

    @DubboReference(interfaceClass = FvsDeviceService.class, version = "1.0.0")
    private FvsDeviceService fvsDeviceService;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public ListData<OpenApiFseLiftDTO> list(Integer deptId) {
        ListData<OpenApiFseLiftDTO> listData = fseLiftService.list(deptId);
        List<OpenApiFseLiftDTO> list = listData.getList();
        getVideoUrl(list);
        return listData;
    }

    @Override
    public OpenApiFseLiftDataDTO deviceInfo(String sn) {
        return fseLiftDataService.latest(sn);
    }

    @Override
    public ListData<FseLiftDataLogDTO> dataReal(String sn, Date startTime, Date endTime) {
        return fseLiftDataService.log(sn, startTime, endTime);
    }

    @Override
    public ListData<OpenApiFseLiftRecordDTO> dataRecord(String sn, Date startTime, Date endTime) {
        return fseRecordService.liftRecordList(sn, startTime, endTime);
    }

    @Override
    public void dataAdd(FseLiftDataAddParam param) {
        FseLiftDataDTO fseLiftDataDTO = new FseLiftDataDTO();
        BeanUtils.copyProperties(param, fseLiftDataDTO);
        amqpTemplate.convertAndSend(QueueConst.FSE_LIFT_REAL_DATA, JSONObject.toJSONString(fseLiftDataDTO));
    }

    /**
     * 探越实时数据
     *
     * @param data
     */
    @Override
    public void tanyueRealData(String data) {
        JSONObject json = JSONObject.parseObject(data);
        FseLiftDataDTO fseLiftDataDTO = new FseLiftDataDTO();
        setTanYueLiftData(json, fseLiftDataDTO);
        amqpTemplate.convertAndSend(QueueConst.FSE_LIFT_REAL_DATA, JSONObject.toJSONString(fseLiftDataDTO));
    }

    /**
     * 设置探越实时数据
     *
     * @param
     */
    private void setTanYueLiftData(JSONObject json, FseLiftDataDTO fseLiftDataDTO) {
        Integer sn = json.getInteger("eid");
        // 设备唯一标识
        fseLiftDataDTO.setSn(String.valueOf(sn));
        // 探越
        fseLiftDataDTO.setPlatform(DevicePlatform.tanyue.name());
        // 高度
        Double height = json.getDouble("height");
        fseLiftDataDTO.setHeight(MathUtil.round(height * 1.0, 1));
        // 重量
        Double weight = json.getDouble("weight");
        fseLiftDataDTO.setWeight(MathUtil.round(weight , 3));
        // 速度
        Double speed = json.getDouble("speed");
        fseLiftDataDTO.setSpeed(MathUtil.round(speed , 2));
        // 风速
        Double wind_speed = json.getDouble("wind_speed");
        fseLiftDataDTO.setWindSpeed(MathUtil.round(wind_speed , 2));
        // 人数
        Integer number = json.getInteger("number");
        fseLiftDataDTO.setPersonNo(number);
        // 倾斜
        Double tilt_x = json.getDouble("tilt_x");
        fseLiftDataDTO.setDipAngle(MathUtil.round(tilt_x , 1));
        // 前门
        Integer front_door_status = json.getInteger("front_door_status");
        front_door_status = front_door_status == 1 ? 2 : 0;
        fseLiftDataDTO.setFrontDoorState(front_door_status);
        // 后门
        Integer back_door_status = json.getInteger("back_door_status");
        back_door_status = back_door_status == 1 ? 2 : 0;
        fseLiftDataDTO.setBackDoorState(back_door_status);

        fseLiftDataDTO.setTime(new Date());
    }

    /**
     * 报警数据
     *
     * @param data
     */
    @Override
    public void tanyueWarnData(String data) {
        JSONObject json = JSONObject.parseObject(data);
        FseLiftDataDTO fseLiftDataDTO = new FseLiftDataDTO();
        setTanYueLiftData(json, fseLiftDataDTO);
        // 报警值
        Integer type_id = json.getInteger("type_id");
        Integer integer = changeTanYueAlarmType(type_id);
        if (integer==0){
            logger.info("报警类型错误type_id：{}",type_id);
            return;
        }
        fseLiftDataDTO.setLiftStatus(integer);
        amqpTemplate.convertAndSend(QueueConst.FSE_LIFT_REAL_DATA, JSONObject.toJSONString(fseLiftDataDTO));
    }

    public Integer changeTanYueAlarmType(Integer alarmType){
        if (alarmType==1||alarmType==2) {
            return FseWarnRuleType.LIFT_HIGT.value();
        }
        if (alarmType==3||alarmType==4||alarmType==5||alarmType==6) {
            return FseWarnRuleType.LIFT_WEIGH.value();
        }
        if (alarmType==7||alarmType==8) {
            return FseWarnRuleType.LIFT_WIND_SPEED.value();
        }
        if (alarmType==9||alarmType==10) {
            return FseWarnRuleType.LIFT_TURN.value();
        }
        if (alarmType==11||alarmType==12) {
            return FseWarnRuleType.LIFT_SPEED.value();
        }
        if (alarmType==13) {
            return FseWarnRuleType.LIFT_PERSON.value();
        }
        return 0;
    }
    /**
     * 循环工作数据
     *
     * @param data
     */
    @Override
    public void tanyueWorkData(String data) {
        JSONObject json = JSONObject.parseObject(data);
        FseLiftRecordDTO fseLiftDataDTO = new FseLiftRecordDTO();
        Integer sn = json.getInteger("eid");
        // 设备唯一标识
        fseLiftDataDTO.setSn(String.valueOf(sn));
        // 探越
        fseLiftDataDTO.setPlatform(DevicePlatform.tanyue.name());
        // 重量
        Double weight = json.getDouble("weight");
        fseLiftDataDTO.setWeight(MathUtil.round(weight , 3));
        // 开始高度
        Double start_height = json.getDouble("start_height");
        fseLiftDataDTO.setStartHeight(MathUtil.round(start_height , 2));
        // 结束高度
        Double end_height = json.getDouble("end_height");
        fseLiftDataDTO.setEndHeight(MathUtil.round(end_height , 2));
        // 人数
        Integer number = json.getInteger("number");
        fseLiftDataDTO.setPersonNo(number);
        // 速度
        Double speed = json.getDouble("speed");
        fseLiftDataDTO.setAvgSpeed(MathUtil.round(speed , 2));
        // 倾角x轴
        Double start_tilt_x = json.getDouble("start_tilt_x");
        Double end_tilt_x = json.getDouble("end_tilt_x");
        Double tilt_x = end_tilt_x > start_tilt_x ? end_tilt_x - start_tilt_x : start_tilt_x - end_tilt_x;
        fseLiftDataDTO.setDipAngleX(MathUtil.round(tilt_x , 1));
        // 倾角y轴
        Double start_tilt_y = json.getDouble("start_tilt_y");
        Double end_tilt_y = json.getDouble("end_tilt_y");
        Double tilt_y = end_tilt_y > start_tilt_y ? end_tilt_y - start_tilt_y : start_tilt_y - end_tilt_y;
        fseLiftDataDTO.setDipAngleY(MathUtil.round(tilt_y , 1));
        // 开始时间
        Date start_time = json.getDate("start_time");
        fseLiftDataDTO.setStartTime(start_time);
        // 结束时间
        Date end_time = json.getDate("end_time");
        fseLiftDataDTO.setEndTime(end_time);
        amqpTemplate.convertAndSend(QueueConst.FSE_LIFT_RECORD_DATA, JSONObject.toJSONString(fseLiftDataDTO));
    }

    private void getVideoUrl(List<OpenApiFseLiftDTO> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        list.forEach(openApiFseLiftDTO -> {
            List<OpenApiFseDeviceDTO> videoList = openApiFseLiftDTO.getVideoList();
            if (videoList != null && !videoList.isEmpty()) {
                videoList.forEach(openApiFseDeviceDTO -> {
                    Integer deviceId = openApiFseDeviceDTO.getDeviceId();
                    FvsStreamDTO fvsStreamDTO = fvsDeviceService.getStreamUrl(deviceId);
                    openApiFseDeviceDTO.setHls(fvsStreamDTO.getHls());
                    openApiFseDeviceDTO.setHlsHd(fvsStreamDTO.getHlsHd());
                });
            }
        });
    }
}
